package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Area;
import com.itgaoshu.hospitalproject.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;




    //查询生产地址的方法
   @RequestMapping("findAllArea")
    @ResponseBody
    public Object AreaList(Area area,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Area> areas = areaService.findAllArea(area);
        PageInfo pageInfo = new PageInfo(areas);
        Map<String,Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code",0);
        tableData.put("msg","");
        //将全部条数作为count传给前台
        tableData.put("count",pageInfo.getTotal());
        //将分页后的数据返回
        tableData.put("data",pageInfo.getList());
        return tableData;
    }

    //添加生产地址的方法
    @RequestMapping("addArea")
    @ResponseBody
    public Object addArea(Area area){
        int count = areaService.count(area);
        if(count==0){
            int result = areaService.addArea(area);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return area.getAreaName()+"已经存在";
        }
    }


    //删除
    @RequestMapping("deleteArea")
    @ResponseBody
    public Object deleteArea(int areaId){
        int result = areaService.deleteArea(areaId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

}















